// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Photos.Domain.Entities;

/// <summary>
/// 照片标签实体
/// </summary>
public class PhotoTag : FullAuditedEntity<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public virtual Guid? TenantId { get; protected set; }

    /// <summary>
    /// 照片ID
    /// </summary>
    public virtual Guid PhotoId { get; protected set; }

    /// <summary>
    /// 标签名称
    /// </summary>
    [Required]
    [MaxLength(100)]
    public virtual string Name { get; protected set; } = string.Empty;

    /// <summary>
    /// 标签类型
    /// </summary>
    public virtual TagType Type { get; protected set; }

    /// <summary>
    /// 置信度（0-1）
    /// </summary>
    public virtual double Confidence { get; protected set; }

    /// <summary>
    /// 是否为自动标签
    /// </summary>
    public virtual bool IsAutoGenerated { get; protected set; }

    /// <summary>
    /// 标签来源
    /// </summary>
    [MaxLength(200)]
    public virtual string? Source { get; protected set; }

    /// <summary>
    /// 关联照片
    /// </summary>
    public virtual Photo Photo { get; protected set; } = null!;

    #region 构造函数

    /// <summary>
    /// 参数化构造函数
    /// </summary>
    /// <param name="id">标签ID</param>
    /// <param name="tenantId">租户ID</param>
    /// <param name="photoId">照片ID</param>
    /// <param name="name">标签名称</param>
    /// <param name="type">标签类型</param>
    /// <param name="isAutoGenerated">是否自动生成</param>
    /// <param name="confidence">置信度</param>
    /// <param name="source">来源</param>
    public PhotoTag(
        Guid id,
        Guid? tenantId,
        Guid photoId,
        string name,
        TagType type = TagType.Manual,
        bool isAutoGenerated = false,
        double confidence = 1.0,
        string? source = null) : base(id)
    {
        TenantId = tenantId;
        PhotoId = photoId;
        Name = name;
        Type = type;
        IsAutoGenerated = isAutoGenerated;
        Confidence = Math.Clamp(confidence, 0.0, 1.0);
        Source = source;
    }

    /// <summary>
    /// 无参构造函数（用于EF Core）
    /// </summary>
    protected PhotoTag()
    {
    }

    #endregion

    #region 业务方法

    /// <summary>
    /// 更新置信度
    /// </summary>
    /// <param name="confidence">新置信度</param>
    public virtual void UpdateConfidence(double confidence)
    {
        Confidence = Math.Clamp(confidence, 0.0, 1.0);
    }

    /// <summary>
    /// 更新标签名称
    /// </summary>
    /// <param name="newName">新名称</param>
    public virtual void UpdateName(string newName)
    {
        Name = newName;
    }

    #endregion
}

/// <summary>
/// 标签类型枚举
/// </summary>
public enum TagType
{
    /// <summary>
    /// 手动标签
    /// </summary>
    Manual = 0,

    /// <summary>
    /// AI自动标签
    /// </summary>
    Auto = 1,

    /// <summary>
    /// EXIF标签
    /// </summary>
    Exif = 2,

    /// <summary>
    /// 地理位置标签
    /// </summary>
    Location = 3,

    /// <summary>
    /// 人物标签
    /// </summary>
    Person = 4,

    /// <summary>
    /// 时间标签
    /// </summary>
    Time = 5
}